package com.lagou.edu.mvcframework.interceptor;

import com.lagou.edu.mvcframework.pojo.Handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class InterceptorChain implements HandleInterceptor {


    //拦截器处理链
    private List<HandleInterceptor> handleInterceptorList = new ArrayList<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Handler handler) throws Exception {
        for (HandleInterceptor handleInterceptor : handleInterceptorList ) {
           boolean preHandleBoolean = handleInterceptor.preHandle(request,response,handler);
           if (!preHandleBoolean){
               return false;
           }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Handler handler) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Handler handler) {

    }

    public void addInterceptor(HandleInterceptor handleInterceptor) {
        handleInterceptorList.add(handleInterceptor);
    }
}
